
package com.assistant.core.modules.sys.adapter.web.role;

import com.assistant.core.modules.sys.app.role.cmd.RoleInsertCmd;
import com.assistant.core.modules.sys.app.role.cmd.RoleUpdateCmd;
import com.assistant.core.modules.sys.domain.role.Role;
import com.assistant.core.modules.sys.app.role.RoleService;
import com.assistant.util.MessageBodyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/sys")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping(value = "/role/{id}")
    public MessageBodyWrapper<Role> get(@PathVariable("id") Long id) {
        return MessageBodyWrapper.success(roleService.get(id));
    }

    @GetMapping(value = "/roles")
    public MessageBodyWrapper<List<Role>> list() {
        return MessageBodyWrapper.success(roleService.list());
    }

    @PostMapping(value = "/role")
    public MessageBodyWrapper<Object> insert(@RequestBody RoleInsertCmd cmd) {
        roleService.insert(cmd);
        return MessageBodyWrapper.success();
    }

    @PutMapping(value = "/role/{id}")
    public MessageBodyWrapper<Object> update(@PathVariable("id") Long id, @RequestBody RoleUpdateCmd cmd) {
        cmd.setId(id);
        roleService.update(cmd);
        return MessageBodyWrapper.success();
    }

    @DeleteMapping(value = "/role/{id}")
    public MessageBodyWrapper<Object> update(@PathVariable("id") Long id) {
        roleService.delete(id);
        return MessageBodyWrapper.success();
    }
}

